package leetcode.code2135;


import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution2135 implements IDebug {

	int basic = 0b1111_1111_1111_1111_1111_111111;
	char[] dics = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
			's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

	public int wordCount(String[] startWords, String[] targetWords) {
		Set<Integer> hash = new HashSet<>();
		for (String start : startWords) {
			int cur = 0;
			for (int i = 0; i < start.length(); i++) {
				char c = start.charAt(i);
				int index = c - 'a';
				cur |= 1 << index;
			}
			int diff = basic ^ cur;
			int move = 0;
			while (diff > 0) {
				int last = diff & 1;
				if (last == 1) {
					hash.add(cur | 1 << move);
				}
				diff >>= 1;
				move++;
			}
		}
		int ans = 0;
		for (String target : targetWords) {
			int cur = 0;
			for (int i = 0; i < target.length(); i++) {
				char c = target.charAt(i);
				int index = c - 'a';
				cur |= 1 << index;
			}
			if (hash.contains(cur)) {
				ans++;
			}
		}
		return ans;
	}

	public int wordCount1(String[] startWords, String[] targetWords) {
		Set<Integer> hash = new HashSet<>();
		Set<String> hash1 = new HashSet<>();
		for (String start : startWords) {
			int cur = 0;
			for (int i = 0; i < start.length(); i++) {
				char c = start.charAt(i);
				int index = c - 'a';
				cur |= 1 << index;
			}
			hash.add(cur);
			int diff = basic ^ cur;
			int move = 0;
			while (diff > 0) {
				int last = diff & 1;
				if (last == 1) {
					hash1.add(start + dics[move]);
				}
				diff >>= 1;
				move++;
			}
		}
		int ans = 0;
		for (String target : targetWords) {
			int cur = 0;
			for (int i = 0; i < target.length(); i++) {
				char c = target.charAt(i);
				int index = c - 'a';
				cur |= 1 << index;
			}
			if (hash.contains(cur) || hash1.contains(target)) {
				ans++;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(2, this.wordCount(H.str2array2("[\"ant\",\"act\",\"tack\"]"),
				H.str2array2("[\"tack\",\"act\",\"acti\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2135 so = new Solution2135();
		so.debug4();

	}

}
